package ebedynky.controllers;

import ebedynky.beans.*;
import ebedynky.services.*;
import ebedynky.validators.DeliveryPlaceValidator;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import javax.validation.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.*;
import org.springframework.validation.*;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

/**
 * @author Ondrej David (davidon1)
 */
@Controller
public class DeliveryPlaceController {

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setValidator(new DeliveryPlaceValidator());
    }

    /**
     * Fetches the DeliveryPlace adding page when requested.
     * @param model data holder for this view
     * @return string representing page location
     */
    @RequestMapping(value = "/deliveryPlaceAdd", method = RequestMethod.GET)
    public String getCreateForm(Model model) {
        model.addAttribute(new DeliveryPlace());
        return "offer/deliveryplace/createForm";
    }

    /**
     * Extracts the DeliveryPlace data from the form,
     * validates them and stores them into the db.
     * @param deliveryPlace data from the form
     * @param result data binding result
     * @return string representing next (success/failure) page location
     */
    @RequestMapping(value = "/deliveryPlaceAdd", method = RequestMethod.POST)
    public String create(@Valid DeliveryPlace deliveryPlace, BindingResult result) {
        if (result.hasErrors()) {
            return "offer/deliveryplace/createForm";
        }
        DeliveryPlaceService dps = new DeliveryPlaceService();
        dps.addDeliveryPlace(deliveryPlace);
        return "offer/index";
    }

    /**
     * Fetches the DeliveryPlace delete page when requested.
     * @param model data holder for this view
     * @return string representing page location
     */
    @RequestMapping(value = "/deliveryPlaceDel", method = RequestMethod.GET)
    public String getDeleteForm(Model model) {
        model.addAttribute(new DeliveryPlace());
        return "offer/deliveryplace/deleteForm";
    }

    /**
     * Deletes the corresponding DeliveryPlace data from the db.
     * @param deliveryPlace data from the form
     * @param result data binding result
     * @return string representing next (success/failure) page location
     */
    @RequestMapping(value = "/deliveryPlaceDel", method = RequestMethod.POST)
    public String delete(@Valid DeliveryPlace deliveryPlace, BindingResult result) {
        if (result.hasErrors()) {
            return "offer/deliveryplace/deleteForm";
        }
        DeliveryPlaceService dps = new DeliveryPlaceService();
        dps.deleteDeliveryPlace(deliveryPlace.getDeliveryPlaceID());
        return "offer/index";
    }

    /**
     * Adds a DeliveryPlace list extracted from the db as an attribute to the model.
     * @return List<DeliveryPlace> list of DeliveryPlaces
     */
    @ModelAttribute("deliveryPlaceList")
    protected List<DeliveryPlace> deliveryPlaceData() {
        DeliveryPlaceService dps = new DeliveryPlaceService();
        List<DeliveryPlace> list = dps.getAllDeliveryPlaces();
        return list;
    }
}
